package spring.lottery.controller;

import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import spring.lottery.common.exception.ServerException;
import spring.lottery.common.pojo.ResponseMode;
import spring.lottery.common.util.SerializeUtil;
import spring.lottery.controller.param.CreatePrizeParam;
import spring.lottery.controller.param.PageParam;
import spring.lottery.server.ActivitiesServer;
import spring.lottery.server.PicServer;
import spring.lottery.server.exceptionCode.ServerConstantCode;
import spring.lottery.server.imp.ActivitiesServerImp;

@Slf4j
@RestController
@RequestMapping("prize")
public class PrizeController {

    @Autowired
    PicServer picServer;
    @Autowired
    ActivitiesServer activitiesServer;

    /**
     * 返回活动id
     */
    @RequestMapping("create")
    public ResponseMode create(@Validated @RequestParam("param") String param,
                               @RequestParam("prizePic") MultipartFile pic) {
        log.info("收到请求 create: {} || {}", param, pic.getName());
        CreatePrizeParam createPrizeParam = SerializeUtil.unEncode(param, CreatePrizeParam.class);

        String path = picServer.savaPic(pic);

        if(!StringUtils.hasLength(path)) {
            throw new ServerException(ServerConstantCode.SAVE_IMG_REE,"path 空");
        }
        log.info("图片储存的路径 {}", path);

        createPrizeParam.setImageUrl(path);

        //储存到数据库中去
        return ResponseMode.success(activitiesServer.create(createPrizeParam));
    }

    /**
     * 展示奖品
     * currentPage: currentPage,
     * pageSize: pageSize
     */
    @RequestMapping("find-list")
    public ResponseMode prizePrizeFindList(@Validated PageParam param) {
        log.info("收到请求 prizePrizeFindList: {}", param);
        return ResponseMode.success(activitiesServer.prizePrizeFindList(param));

    }
}
